package com.geega.web.handler;

import com.geega.web.core.ApiResponse;
import com.geega.web.core.BizException;
import com.geega.web.core.ErrorId;
import com.geega.web.core.ErrorStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.List;

/**
 * 全局异常处理
 */
@Slf4j
@RestControllerAdvice(basePackages = "com.geega.web")
public class GlobalExceptionHandler {

    /**
     * 捕获未知异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public Object Exception(Exception e) {
        ErrorId errorId = ErrorId.get();
        log.error("操作失败：errorId = {}", errorId.getErrorId(), e);
        return ApiResponse.fail(ErrorStatus.SERVER_ERROR_CODE, ErrorStatus.SERVER_ERROR_MSG, errorId);
    }

    /**
     * 捕获业务处理异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = BizException.class)
    public Object BizException(BizException e) {
        return ApiResponse.fail(ErrorStatus.DEFAULT_ERROR_CODE, e.getMessage());
    }

    /**
     * 请求方法错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public Object HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
        return ApiResponse.fail(ErrorStatus.REQUEST_METHOD_ERROR_CODE, ErrorStatus.REQUEST_METHOD_ERROR_MSG);
    }

    /**
     * 请求参数缺失
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = MissingServletRequestParameterException.class)
    public Object MissingServletRequestParameterException(MissingServletRequestParameterException e) {
        return ApiResponse.fail(ErrorStatus.REQUEST_ERROR_CODE, ErrorStatus.REQUEST_ERROR_MSG);
    }

    /**
     * 请求体参数错误
     *
     * @param e
     * @return
     */
    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public Object HttpMessageNotReadableException(HttpMessageNotReadableException e) {
        return ApiResponse.fail(ErrorStatus.REQUEST_ERROR_CODE, ErrorStatus.REQUEST_ERROR_MSG);
    }

    /**
     * 捕获参数验证异常
     *
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Object MethodArgumentNotValidException(MethodArgumentNotValidException e) {
        BindingResult exceptions = e.getBindingResult();
        if (exceptions.hasErrors()) {
            List<ObjectError> errors = exceptions.getAllErrors();
            if (!errors.isEmpty()) {
                FieldError fieldError = (FieldError) errors.get(0);
                return ApiResponse.fail(ErrorStatus.REQUEST_ERROR_CODE, fieldError.getDefaultMessage());
            }
        }
        return ApiResponse.fail(ErrorStatus.REQUEST_ERROR_CODE, ErrorStatus.REQUEST_ERROR_MSG);
    }
}
